
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
/**
 * 玩家管理类
 */
public class PlayerMananger : MonoBehaviour
{
    //玩家生命值
    public int lifeValue = 3;
    //玩家得分
    public int playerScore = 0;
    //玩家死亡
    public bool isDead;
    //游戏失败
    public bool isDefeat = false;
    // public bool isGameOver;

    //出生点
    public GameObject born;

    //得分
    public Text PlayerScoreText;
    //生命值
    public Text PlayerLifeVlaue;
    //游戏结束的效果
    public GameObject isDefeateUI;


    //单例
    private static PlayerMananger instance;

    public static PlayerMananger Instance
    {
        get { return instance; }
        set { instance = value; }
    }

    private void Awake()
    {
        instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        //游戏结束，更换ui显示
        if (isDefeat)
        {
            isDefeateUI.SetActive(true);

            // 延迟指定时间后跳转场景
            Invoke("ChangeScene", 3);
            return;
        }

        if (isDead)
        {
            //调用复活
            Recover();
        }

        //动态更改 得分 
        PlayerScoreText.text = playerScore.ToString();
        //动态更改 生命值
        PlayerLifeVlaue.text = lifeValue.ToString();

    }

    //复活
    private void Recover()
    {
        if (lifeValue <= 0)
        {
            //游戏失败，返回主界面
            isDefeat = true;
            //回到主界面
            Invoke("ReturnToTheMainMenu", 3);
        }
        else
        {
            lifeValue--;

            GameObject go = Instantiate(born, new Vector3(-2, -8, 0), Quaternion.identity);
            go.GetComponent<Born>().createPlayer = true;
            isDead = false;
        }

    }

    private void ReturnToTheMainMenu()
    {
        //加载第一个场景
        SceneManager.LoadScene(1);
    }

    /**
     * 游戏失败，返回主界面
     */
    private void ChangeScene()
    {
        SceneManager.LoadScene(0);
    }
}
